package cn.com.dhc.danlu.widget.base;

import android.content.Context;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import java.util.List;

import cn.com.dhc.danlu.dlware.net.GeneralBusinessObserver;
import cn.com.dhc.danlu.util.ToastUtil;
import cn.com.dhc.danlu.widget.CustomAbstractListView;
import cn.com.dhc.danlu.widget.adapter.MyBaseAdapter;
import cn.com.dhc.danlu.widget.loadlayer.LoaddingLayout;
import cn.com.dhc.mibd.eufw.widget.android.ptr.CustomPullToRefreshListView;
import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshBase;
import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshBase.OnRefreshListener2;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetMyCouponListResponse;

/**
 * @author wuhaiyang
 */
public abstract class BaseListViewLogic<RequestModel, ResultData> extends CustomAbstractListView implements OnRefreshListener2 {
    /**
     * 当前页码
     */
    protected int mPage = 1;
    protected boolean isPullDownRefresh = true;

    protected MyBaseAdapter mAdapter;

    protected LoaddingLayout mLoaddingLayout;
    protected OnClickListener retryListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            mLoaddingLayout.showLoaddingView();
            isPullDownRefresh = true;
            callFetchData();
        }
    };
    public class ListObserver extends GeneralBusinessObserver<GetMyCouponListResponse> {
        @Override
        protected void onSuccess(GetMyCouponListResponse model) {
            if (null != model.getCouponList() && !model.getCouponList().isEmpty()) {
                if (isPullDownRefresh) {
                    mPage = 2;
                    mAdapter.refreshData(model.getCouponList());
                } else {
                    mAdapter.appendData(model.getCouponList());
                    mPage++;
                }
                mLoaddingLayout.showContentView();
            } else {
                if (!isPullDownRefresh) {
                    ToastUtil.getInstance().show("暂无更多红包");
                } else {
                    modifyEmptyReloadShowText();
                    mLoaddingLayout.showEmptyView();
                }
            }
            ptrListView.onRefreshComplete();
        }

        @Override
        protected void onAlertTips(String displayMessage) {
            displayMessage = TextUtils.isEmpty(displayMessage) ? "数据请求失败" : displayMessage;
            ptrListView.onRefreshComplete();
            if (isPullDownRefresh) {
                modifyErrorReloadText();
                mLoaddingLayout.showErrorView(displayMessage);
            }
        }
    };

    //    public BaseListViewLogic(AsyncTaskActivityDelegate delegate, CustomPullToRefreshListView ptrListView) {
    //        super(delegate, ptrListView);
    //        handleProcess();
    //    }
    public BaseListViewLogic(Context context, CustomPullToRefreshListView ptrListView) {
        super(context, ptrListView);
        handleProcess();
    }

    protected void handleProcess() {
        mAdapter = (MyBaseAdapter) initAdapter();
        ptrListView.getRefreshableView().setAdapter(mAdapter);
        ptrListView.setOnRefreshListener(this);
    }

    protected abstract BaseAdapter initAdapter();

    public void setLoaddingLayout(LoaddingLayout mLoaddingLayout) {
        this.mLoaddingLayout = mLoaddingLayout;
        // 设置默认加载失败 的情况 点击按钮重新加载
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        isPullDownRefresh = true;
        // 重新请求数据
        callFetchData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isPullDownRefresh = false;
        // 重新请求数据
        callFetchData();
    }

    /***
     * 通知子类去实现
     */
    public void callFetchData() {
    }

    public Context getContext() {
        return ptrListView.getContext();
    }

    /**
     * 针对可以下拉刷新的情况使用
     */
    protected void startLoad() {
        ptrListView.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptrListView.setRefreshing(true);
            }
        }, 200);
    }

    protected abstract List getList(ResultData model);

    /**
     * 数据为空的  修改textView 的text
     */
    protected void modifyEmptyReloadShowText() {
    }

    /**
     * 数据加载失败
     */
    protected void modifyErrorReloadText() {
    }

    @Override
    public void onItemClickListener(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    }
}